home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / C-H / Finder Progress C.cpt / Finder Progress folder / Finder Progress.c next >
C/C++ Source or Header  |  1993-01-02  |  5KB  |  212 lines

  1. /*
  2.     Finder Progress 
  3.     
  4.     by Joe Zobkiw
  5.     
  6.     This is a simple THINK C 5.0.4 project and compiled application 
  7.     that shows you how to (very easily) implement a Finder 7-like 
  8.     progress meter. I have only seen cheap imitations on the nets so 
  9.     I decided to code one that looks a little closer to how the Finder 
  10.     does.
  11.     
  12.     You can extend this code in any way you wish, make it movable modal, 
  13.     do with it what you will, it should work in almost any scenario.
  14.     
  15.     If you make any changes please send them to me at any of the 
  16.     following addresses. Enjoy, and please give this to your friends.
  17.     
  18.     America Online: AFL Zobkiw
  19.     Internet: zobkiw@world.std.com
  20. */
  21.  
  22. #include "Finder Progress.h"
  23.  
  24. main()
  25. {
  26.     DialogPtr    d = nil;
  27.     GrafPtr        savePort = nil;
  28.     Rect        r;
  29.     short        i;
  30.     short        bitDepth;
  31.     long        someTime;
  32.     
  33.     /* initialize the toolbox */
  34.     InitGraf(&thePort);
  35.     InitFonts();
  36.     InitWindows();
  37.     InitMenus();
  38.     TEInit();
  39.     InitDialogs(0L);
  40.     InitCursor();
  41.     GetDateTime(&randSeed);
  42.     FlushEvents(everyEvent,0);
  43.     DrawMenuBar();
  44.  
  45.     /* show the about alert */
  46.     Alert(kAboutAlertID, nil);
  47.  
  48.     /* get the dialog box */
  49.     d = GetNewDialog(kModalProgressDialogID, nil, (WindowPtr)-1);
  50.     if (d == nil) ExitToShell();
  51.     
  52.     /* set up the port for drawing */
  53.     GetPort(&savePort);
  54.     SetPort(d);
  55.     
  56.     ParamText("\pNow doing something time consuming…","\p","\p","\p");
  57.     
  58.     ShowWindow(d);
  59.     DrawDialog(d);
  60.     
  61.     /* if this isn’t available, we are toast anyway! */
  62.     SetCursor(*GetCursor(watchCursor));
  63.     
  64.     /* get the rectangle of the progress item and the current bitdepth */
  65.     r = DItemRect(d, kDialogProgressItem);
  66.     bitDepth = BitDepth();
  67.     
  68.     /* now we’re making progress! */
  69.     for (i=0;i<=100;++i) {
  70.         UpdateProgress(&r, i, (bitDepth >= kColorBitDepth));    
  71.         Delay(2, &someTime);    
  72.     }
  73.     
  74.     /* clean things up */
  75.     InitCursor();
  76.     ParamText("\p","\p","\p","\p");
  77.     DisposeDialog(d);
  78.     SetPort(savePort);
  79. }
  80.  
  81. /********************************************************************
  82.     UpdateProgress
  83.     
  84.     draws the progress item, expects the port to be set properly.
  85.     You can pass any parameters you like into this routine, it
  86.     depends on the situation you plan on using it in. I decided
  87.     to simply pass a Rect, percentage, and a color flag.
  88.     
  89.     this implementation draws the entire thermometer each time
  90.     which will cause the cursor to flicker if you move it over the
  91.     thermometer itself. i do this for a few reasons, one being i
  92.     didn't want the overhead or hassle of allocating an offscreen
  93.     pixmap or bitmap to provide flicker free animation, and secondly
  94.     if we are drawing and a screensaver goes on, when it kicks out
  95.     again we will be sure to draw the entire thermometer immediately 
  96.     and not just a portion of it.
  97.     
  98. ********************************************************************/
  99. void UpdateProgress(Rect *r, short percentage, Boolean color)
  100. {
  101.     RGBColor    rgb;
  102.     Rect        rect, fillRect, emptyRect;
  103.     PenState    savePen;
  104.     RGBColor    foreColor;
  105.     short        offset;
  106.  
  107.     rect = fillRect = emptyRect = *r;
  108.  
  109.     /* save the pen state and color */
  110.     GetPenState(&savePen);
  111.     if (color) 
  112.         GetForeColor(&foreColor);
  113.     PenNormal();
  114.     
  115.     /* first frame the rectangle */
  116.     if (color) {
  117.         rgb.red = 0;
  118.         rgb.green = 0;
  119.         rgb.blue = 0;
  120.         RGBForeColor(&rgb);
  121.     } else {
  122.         ForeColor(blackColor);
  123.     }
  124.     FrameRect(&rect);
  125.     
  126.     /* figure out the percentage */
  127.     InsetRect(&rect, 1, 1);
  128.     InsetRect(&fillRect, 1, 1);
  129.     InsetRect(&emptyRect, 1, 1);
  130.     offset = (rect.right - rect.left) * percentage / 100;
  131.     
  132.     /* first draw the filled in portion */
  133.     fillRect.right = fillRect.left + offset;
  134.     if (color) {
  135.         rgb.red = kGrayProgressColorRed;
  136.         rgb.green = kGrayProgressColorGreen;
  137.         rgb.blue = kGrayProgressColorBlue;
  138.         RGBForeColor(&rgb);
  139.     } else {
  140.         ForeColor(blackColor);
  141.     }
  142.     PaintRect(&fillRect);
  143.     
  144.     /* now draw the unfilled portion */
  145.     emptyRect.left = fillRect.right + 1;
  146.     if (color) {
  147.         rgb.red = kPurpleProgressColorRed;
  148.         rgb.green = kPurpleProgressColorGreen;
  149.         rgb.blue = kPurpleProgressColorBlue;
  150.         RGBForeColor(&rgb);
  151.     } else {
  152.         ForeColor(whiteColor);
  153.     }
  154.     PaintRect(&emptyRect);
  155.     
  156.     /* restore pen settings, colors, restore to black when not in color */
  157.     SetPenState(&savePen);
  158.     if (color) {
  159.         RGBForeColor(&foreColor);
  160.     } else {
  161.         ForeColor(blackColor);
  162.     }
  163. }
  164.  
  165. /********************************************************************
  166.     DItemRect
  167.     
  168.     returns the rectangle of a dialog item
  169. ********************************************************************/
  170. Rect DItemRect(DialogPtr dialog, short item)
  171. {
  172.     short    itemType;
  173.     Handle    itemHandle;
  174.     Rect    itemRect;
  175.     
  176.     GetDItem(dialog, item, &itemType, &itemHandle, &itemRect);
  177.     return itemRect;    
  178. }
  179.  
  180. /********************************************************************
  181.     ColorQDIsPresent
  182.     
  183.     returns true if color quickdraw is present, false otherwise
  184. ********************************************************************/
  185. Boolean    ColorQDIsPresent(void)
  186. {
  187.     SysEnvRec    theWorld;
  188.     
  189.     SysEnvirons(1, &theWorld);
  190.     return(theWorld.hasColorQD);
  191. }
  192.  
  193. /********************************************************************
  194.     BitDepth
  195.     
  196.     returns the bitdepth of the main monitor
  197. ********************************************************************/
  198. short BitDepth(void)
  199. {
  200.     short    bits = 1;
  201.     
  202.     if (ColorQDIsPresent()) {
  203.         GDHandle        mainDevice;
  204.         PixMapHandle    pixMap;
  205.         mainDevice = GetMainDevice();
  206.         pixMap = (**mainDevice).gdPMap;
  207.         bits = (**pixMap).pixelSize;
  208.     }
  209.     
  210.     return bits;
  211. }
  212.